﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace test_protobuf
{
    class EchoClient
    {
        protected static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public static void test()
        {
            for (int i = 0; i < 100; ++i)
            {
                Thread th = new Thread(new ParameterizedThreadStart(Test_ThreadProc));
                th.Start(i);
            }

        }
        public static void Test_ThreadProc(Object obj)
        {
            int id = (int)obj;
            WC.Net.Link lnk = new WC.Net.Link();

            try
            {
                log.Debug("[lnk1] Connect...");
                if (!lnk.Connect("localhost", 1234))
                {
                    log.Error("lnk1.Connect() failed");
                    lnk.Close();
                    return;
                }
                log.Debug("lnk.Connect() succeeded");
            }
            catch (System.Net.Sockets.SocketException e)
            {
                log.ErrorFormat("[Test_ThreadProc] Exception: {0}", e.Message);
            }

            if (lnk.IsConnected)
            {
                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                int max_msg = 50;
                for (int i = 1; i <= max_msg; ++i)
                {
                    string msg = "hello~~~~~~~~~~~~" + i;
                    byte[] buffer = encoding.GetBytes(msg);
                    lnk.Send(buffer, buffer.Length, null);
                    log.DebugFormat("[{0}] sent msg: {1}/{2}", id, i, max_msg);
                }
            }
        }
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // http://code.google.com/p/protobuf-net/
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    class Protobuf_net
    {
        protected static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public static void test()
        {
            persion.Person obj1 = new persion.Person();
            {
                obj1.name = "Lynn Kang";
                obj1.id = 123;
                obj1.email = "test@gmail.com";

                persion.Person.PhoneNumber phone;
                phone = new persion.Person.PhoneNumber();
                phone.type = persion.Person.PhoneType.HOME;
                phone.number = "02-111-1111";
                obj1.phone.Add(phone);
                phone = new persion.Person.PhoneNumber();
                phone.type = persion.Person.PhoneType.MOBILE;
                phone.number = "010-222-2222";
                obj1.phone.Add(phone);
            }

            persion.Person obj2 = new persion.Person();
            persion.Person obj3 = new persion.Person();

            // ProtoBuf.Serializer.Serialize
            using (var wfile = System.IO.File.Create("protobuf_net_test1.tmp"))
            {
                ProtoBuf.Serializer.Serialize<persion.Person>(wfile, obj1);
            }
            // ProtoBuf.Serializer.Deserialize
            using (var rfile = System.IO.File.OpenRead("protobuf_net_test1.tmp"))
            {
                obj2 = ProtoBuf.Serializer.Deserialize<persion.Person>(rfile);
            }

            //System.Xml.Serialization.XmlSerializer.Serialize
            using (var wstream = new System.IO.FileStream("protobuf_net_test2.tmp", System.IO.FileMode.Create))
            {
                System.Xml.Serialization.XmlSerializer sr = new System.Xml.Serialization.XmlSerializer(typeof(persion.Person));
                sr.Serialize(wstream, obj2);
            }
            // System.Xml.Serialization.XmlSerializer.Deserialize
            using (var rstream = new System.IO.FileStream("protobuf_net_test2.tmp", System.IO.FileMode.Open))
            {
                System.Xml.Serialization.XmlSerializer sr = new System.Xml.Serialization.XmlSerializer(typeof(persion.Person));
                obj3 = (persion.Person)sr.Deserialize(rstream);
            }

            // ProtoBuf.Serializer.Serialize
            using (var wfile = System.IO.File.Create("protobuf_net_test3.tmp"))
            {
                ProtoBuf.Serializer.Serialize<persion.Person>(wfile, obj3);
            }
        }
    }
    /*
    class Protobuf
    {
        class Person
        {
            public int Id {get;set;}
            public string Name { get; set; }
            public Address Address {get;set;}
        }
        class Address
        {
            public string Line1 {get;set;}
            public string Line2 {get;set;}
        }

        public static void test()
        {
            Person person = new Person();
            person.Id = 123;
            person.Name = "abc";
            Address addr = new Address();
            person.Address = addr;
            using (var file = System.IO.File.Create("person.bin"))
            {
               System.Web.Services.Description.Serializer.Serialize<Person>(file, person);
            }
        }

    }
     * */

    class Program
    {
        protected static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        static void Initialize()
        {
            log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("Pilot.NET.log4net.xml"));
        }

        static void Main(string[] args)
        {
            Initialize();

            //EchoClient.test();
            Protobuf_net.test();

            Console.WriteLine("--------------------------------------------------------------------------");
            Console.WriteLine("Press a key to exit...");
            Console.WriteLine("--------------------------------------------------------------------------");
            Console.ReadKey();
        }
    }
}
